package com.mustafaiev.tair.autobase.jsptag;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.log4j.Logger;

import com.mustafaiev.tair.autobase.dao.OrderDao;
import com.mustafaiev.tair.autobase.dao.OrderDaoImpl;
import com.mustafaiev.tair.autobase.order.ClientOrder;

public class OrdersTag extends TagSupport {

    private static final Logger LOG = Logger.getLogger(OrdersTag.class.getName());
    private int orderId;
    private static final SimpleDateFormat FORMAT = new SimpleDateFormat("dd MMMMM yyyy, kk:mm");

    public void setOrderId(final int orderId) {
        this.orderId = orderId;
    }

    public int getOrderId() {
        return orderId;
    }

    @Override
    public int doStartTag() throws JspException {

        final OrderDao dao = new OrderDaoImpl();

        List<ClientOrder> orders = null;

        JspWriter w = pageContext.getOut();

        final StringBuffer buffer = new StringBuffer(1000);
        try {
            // table begin
            buffer.append("<table class=\"orders\">");
            buffer.append("<thead>");
            buffer.append("<tr>");
            buffer.append("<th>Client Name</th>");
            buffer.append("<th>Client Phone</th>");
            buffer.append("<th>Driver</th>");
            buffer.append("<th>Order</th>");
            buffer.append("<th>Date</th>");
            buffer.append("</tr>");
            buffer.append("</thead>");

            orders = dao.getAllOrders();
            Date date = new Date();
            Iterator<ClientOrder> it = orders.iterator();
            while (it.hasNext()) {
                ClientOrder order = (ClientOrder) it.next();

                buffer.append("<tr id=\"" + order.getId() + "\" class=\"order_item\"  onclick=\"editOrder(this);\">");
                buffer.append("<td class=\"c_name\">");
                buffer.append(order.getName());
                buffer.append("</td>");
                buffer.append("<td class=\"c_phone\">");
                buffer.append(order.getPhone());
                buffer.append("</td>");
                buffer.append("<td class=\"driver\">");
                buffer.append(order.getDriverName());
                buffer.append("</td>");
                buffer.append("<td class=\"order_condition\">");
                buffer.append(order.getOrder());
                buffer.append("</td>");
                buffer.append("<td class=\"order_date\">");
                date.setTime(order.getCreateDate());
                buffer.append(FORMAT.format(date.getTime()));
                buffer.append("</td>");
                buffer.append("</tr>");

            }

            // end of orders table
            buffer.append("</table>");

            // print table
            w.println(buffer.toString());

        } catch (Exception e) {
            throw new JspTagException("Orders tag: " + e);
        }

        return SKIP_BODY;

    }

    @Override
    public int doEndTag() throws JspException {

        return EVAL_PAGE;
    }

}
